/*
**************************************************************************************
*Myriad Engine                                                                       *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
package leola.backend.interpreter;

import leola.backend.EvalException;
import leola.types.LeoBoolean;
import leola.types.LeoNativeClass;
import leola.types.LeoNull;
import leola.types.LeoNumber;
import leola.types.LeoObject;
import leola.types.LeoString;
import leola.utils.ClassUtil;

/**
 * Converts Java types to Leola types and vice versa. 
 * 
 * @author Tony
 *
 */
public class LeoTypeConverter {

	/**
	 * Converts the supplied java object into a {@link LeoObject}.
	 * 
	 * @param javaObj
	 * @return
	 * @throws EvalException
	 */
	public static LeoObject convertToLeolaType(Object javaObj) throws EvalException {
		LeoObject result = null;
		if ( javaObj == null ) {
			result = LeoNull.LEONULL;
		}
		else {
			Class<?> type = javaObj.getClass();
			if ( type.equals(String.class) ) {
				result = new LeoString(javaObj.toString());
			}
			else if ( ClassUtil.isType(type, boolean.class, Boolean.class) ){
				result = ((Boolean)javaObj) ? LeoBoolean.LEOTRUE 
											: LeoBoolean.LEOFALSE;
			}
			else if ( ClassUtil.isType(type, byte.class, Byte.class
									  , short.class, Short.class
									  , int.class, Integer.class
									  , float.class, Float.class
									  , double.class, Double.class
									  , long.class, long.class) ){
				
				Double number = new Double( ((Number)javaObj).doubleValue() );
				result = new LeoNumber(number);
			}
			else if ( ClassUtil.inheritsFrom(type, LeoObject.class)) {
				result = (LeoObject)javaObj;
			}
			else {
				result = new LeoNativeClass(type, javaObj);
			}
		}
		
		return result;
	}
	
	/**
	 * Convert to the specified type.
	 * 
	 * @param v
	 * @param type
	 * @param obj
	 * @throws Exception
	 */
	public static void convertLeoObjectToJavaObj(JavaTypeVisitor v
											   , Class<?> type
											   , LeoObject obj) throws Exception {
		
		if ( type.equals(String.class) ) {
			v.onString(obj);
		}
		else if ( ClassUtil.isType(type, boolean.class, Boolean.class) ){
			v.onBoolean(obj);
		}
		else if ( ClassUtil.isType(type, byte.class, Byte.class) ){
			v.onByte(obj);
		}
		else if ( ClassUtil.isType(type, short.class, Short.class) ){
			v.onShort(obj);
		}
		else if ( ClassUtil.isType(type, int.class, Integer.class) ){
			v.onInt(obj);
		}
		else if ( ClassUtil.isType(type, float.class, Float.class) ){
			v.onFloat(obj);
		}
		else if ( ClassUtil.isType(type, double.class, Double.class) ){
			v.onDouble(obj);
		}
		else if ( ClassUtil.isType(type, long.class, long.class) ){
			v.onLong(obj);
		}
		else {
			v.onObject(obj);
		}
	}
	//public LeoObject convertPrimitive
	
	
	public interface JavaTypeVisitor {
		public void onBoolean(LeoObject obj) throws Exception;
		public void onByte(LeoObject obj) throws Exception;
		public void onShort(LeoObject obj) throws Exception;
		public void onInt(LeoObject obj) throws Exception;
		public void onFloat(LeoObject obj) throws Exception;
		public void onDouble(LeoObject obj) throws Exception;
		public void onLong(LeoObject obj) throws Exception;
		
		public void onString(LeoObject obj) throws Exception;
		public void onObject(LeoObject obj) throws Exception;
	}
}
